<?php

namespace app\modules\admin\controllers;

use yii\web\Controller;
use app\models\LoginForm;
use app\models\SignupForm;
use Yii;
use app\models\Client;

/**
 * Default controller for the `admin` module
 */
class DefaultController extends Controller
{

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction'
            ]
        ];
    }

    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index',[
            'clientCount' => Client::find()->count(),
        ]);
    }

    public function actionLogin()
    {
        if(!Yii::$app->user->isGuest){
            return $this->redirect(['index']);
        }
        $model = new LoginForm();
        if($model->load(Yii::$app->request->post()) && $model->login()){
//            Yii::$app->session->setFlash('success','登录成功');
            return $this->redirect(['index']);
        }
        return $this->render('login',['model'=>$model]);
    }



    public function actionLogout(){
        Yii::$app->user->logout();
        return $this->redirect(['login']);
    }
}
